package com.omega_r.omegarecyclerview.expandable_example.support_sticky;

import com.omega_r.libs.omegarecyclerview.base.BaseBean;
import com.omega_r.libs.omegarecyclerview.base.BaseViewHolder;
import com.omega_r.omegarecyclerview.ResourceTable;
import com.omega_r.omegarecyclerview.expandable_example.core.SimpleData;
import ohos.agp.components.*;
import ohos.agp.window.dialog.ToastDialog;

public class DefaultStickyViewHeadHolder extends BaseViewHolder {
    private Text listAdapterItemTxt;
    private Text txtHeadExpandable;
    private DirectionalLayout directionalLayout;
    private DependentLayout dlParent;
    private DirectionalLayout itemTop;


    public DefaultStickyViewHeadHolder(Component itemView, ListContainer listContainer) {
        super(itemView, listContainer);
        listAdapterItemTxt = (Text) findComponentById(ResourceTable.Id_list_adapter_item_txt);
        directionalLayout = (DirectionalLayout) findComponentById(ResourceTable.Id_dir_add);
        dlParent = (DependentLayout) findComponentById(ResourceTable.Id_dl_parent);
        txtHeadExpandable = (Text) findComponentById(ResourceTable.Id_txt_head_expandable);
        itemTop = (DirectionalLayout) findComponentById(ResourceTable.Id_item_top);
    }

    public void updateView(BaseBean baseBean, boolean isCloseOtherItem,
                           boolean isAnimator, int position, OnClickListener onClickListener) {
        super.updateView(baseBean);
        System.out.println("================updateView==============updateView=======");
        SimpleData listAdapterBean = (SimpleData) baseBean;
        if (listAdapterBean.getItemType() == 0) {
            dlParent.setVisibility(Component.HIDE);
            if (listAdapterBean.getmYear() == 1914) {
                itemView.setMarginTop(-itemView.getMarginBottom());
            }
            txtHeadExpandable.setText(listAdapterBean.getmYear() + "");
            txtHeadExpandable.setVisibility(Component.VISIBLE);
        } else if (listAdapterBean.getItemType() == 1) {
            dlParent.setVisibility(Component.VISIBLE);
            txtHeadExpandable.setVisibility(Component.HIDE);
            listAdapterItemTxt.setText(listAdapterBean.getmTitle());
            directionalLayout.removeAllComponents();
            for (int j = 0; j < listAdapterBean.getmQuotes().size(); j++) {
                Component componentChild = LayoutScatter.getInstance(itemView.getContext()).parse(ResourceTable.Layout_item_core_child_adapter,
                        null, false);
                Text childText = (Text) componentChild.findComponentById(ResourceTable.Id_list_adapter_item_txt);
                childText.setText(listAdapterBean.getmQuotes().get(j));
                directionalLayout.addComponent(componentChild);
            }
            setItemExtend(directionalLayout, itemTop, baseBean, isCloseOtherItem, isAnimator, position, onClickListener);
        }
    }
}
